// JavaScript Document
$(function () {
	initAdd();
	//提交
	$(".submit").on("click",function(){
		checkAdd();
	});
	//图片文件内容改变
	$(".uploadFile").on("change",function(){
		$(".imgurl").val($(this).val());
        var reader = new FileReader();
        reader.readAsDataURL(this.files[0]);
        reader.onload = function(e){
            var strbase64 = e.target.result;
            var a = strbase64.split("base64,");
            $('.file_base64').val(a[1]);
        };
        preImg();//显示图片
	});
	//点击浏览图片
	$(".scan").on("click",function(){
		$(".uploadFile").click();	
	});
});
//初始化
function initAdd(){
	$(".crmbrName").val("");
	$(".crmbrTel").val("");
	$(".crmbrQq").val("");
}
//检查提交
function checkAdd(){
	if($.trim($(".crmbrName").val()) == ""){
		alert("名字不能为空，请选择再提交！");
		$(".crmbrName").focus();
		return false;
	}
	
	if($.trim($(".crmbrTel").val()) == ""){
		alert("电话号码不能为空，请填写完再提交！");
		$(".crmbrTel").focus();
		return false;
	}

	if($.trim($(".crmbrQq").val()) == ""){
		alert("QQ不能为空，请填写完再提交！");
		$(".crmbrQq").focus();
		return false;
	}
	if($.trim($(".imgurl").val()) == ""){
		alert("图片附件不能为空，请填写完再提交！");
		$(".imgurl").focus();
		return false;
	}
	var r=confirm("是否确认增加？");
	if (r==true){
		var msgObject = parent.layer.msg('处理中，请等待……', {
			icon: 16,
			shade: 0.4,
			time: waitImgTime //（如果不配置，默认是3秒）
		}, function(index){
			//do something
			parent.layer.close(index); 
		});
		uploadPic(msgObject);
	}
}
//上传图片
function uploadPic(msgObject){
	$("#userForm").ajaxSubmit({
        //定义返回JSON数据，还包括xml和script格式
        type: "POST",
        dataType:'json',
        url: urlfile + "coreAttachment/upload",
        success: function(strdata) {
        	if(strdata.success==true){
        		Add(strdata.data,msgObject);
        	}else {
            	alert(strdata.errMsg);
            }
        }
    });
}
//提交
function Add(uuids,msgObject){
	var crmbrName = $(".crmbrName").val();
	var crmbrTel = $(".crmbrTel").val();
	var crmbrQq = $(".crmbrQq").val();
	
	var str = 'crmbrName='+encodeURIComponent(crmbrName)+'&crmbrTel='+encodeURIComponent(crmbrTel)+'&crmbrQq='+encodeURIComponent(crmbrQq)+'&uuids='+encodeURIComponent(uuids);
	getOData(str,"coreMyButler/add/coreMyButler",{
		fn:function(oData){
			window.parent.refreshList();
			alert("增加成功！");
		},
		fnerr:function(oData){
			parent.layer.close(msgObject);
		}
	});
}